//
//  ZCXVideoDetialViewController.m
//  LoveOfPet
//
//  Created by dllo on 15/11/14.
//  Copyright © 2015年 孔维. All rights reserved.
//

#import "ZCXVideoDetialViewController.h"
#import "ZFHYNetWork.h"
#import "ZCXVedioDetialTableViewCell.h"
#import "ZCXUserDetialViewController.h"
#import "CoreDataManager.h"
#import "ZPetVideo.h"
#import <AVFoundation/AVFoundation.h>
#import <SVProgressHUD.h>
#define HEIGHT  [[UIScreen mainScreen]bounds].size.height / 667
#define WIDTH   [[UIScreen mainScreen]bounds].size.width / 375
@interface ZCXVideoDetialViewController ()<UITableViewDataSource, UITableViewDelegate, ZCXVedioDetiaTableViewCellDelegate>
@property (nonatomic, retain) NSDictionary *medioDic;
@property (nonatomic, retain) UITableView *tableView;
@property (nonatomic, retain) CoreDataManager *coreDataManager;
@property (nonatomic, retain) AVPlayer *player;
@end

@implementation ZCXVideoDetialViewController
- (void)dealloc{
    [_tableView release];
    [_ID release];
    [_medioDic release];
    [_coreDataManager release];
    [_player release];
    [super dealloc];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"萌宠详情";
    self.view.backgroundColor = [UIColor whiteColor];
     self.navigationController.navigationBar.tintColor = [UIColor blackColor];
    self.coreDataManager = [CoreDataManager shareCoreDataManager];
//    NSLog(@"fffssfsss%@", self.coreDataManager.applicationDocumentsDirectory);
    self.automaticallyAdjustsScrollViewInsets = NO;
   
    NSString *urlString = [NSString stringWithFormat:@"http://newapi.meipai.com/medias/show.json?id=%@&from=5&square_category=6", self.ID];
    [self loadDataWithString:urlString];
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 64, self.view.frame.size.width, self.view.frame.size.height - 64 * HEIGHT) style:UITableViewStyleGrouped];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    //注册Cell
    [self.tableView registerClass:[ZCXVedioDetialTableViewCell class] forCellReuseIdentifier:@"ZCXvediodetialtableviewcell"];
    [self.view addSubview:_tableView];
    self.tableView.tableFooterView = [[UIView alloc]initWithFrame:CGRectZero];
    
    [_tableView release];
    //收藏按钮
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    [button setImage:[UIImage imageNamed:@"Zcollect"] forState:UIControlStateNormal];
    button.frame  = CGRectMake(0, 0, 50 * WIDTH, 50 * HEIGHT);
    [button addTarget:self action:@selector(collectButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:button];
}
- (void)collectButtonAction:(UIButton *)button{
    
    if (![[NSUserDefaults standardUserDefaults] boolForKey:[NSString stringWithFormat:@"%@", self.ID]]) {
        
        [[NSUserDefaults standardUserDefaults] setBool:YES forKey:[NSString stringWithFormat:@"%@", self.ID]];
        
        ZCXVedioDetialModel *model =  [ZCXVedioDetialModel ZCXVedioDetialModelWithDictionary:self.medioDic];
        
        ZPetVideo *petVideo = [NSEntityDescription insertNewObjectForEntityForName:@"ZPetVideo" inManagedObjectContext:self.coreDataManager.managedObjectContext];
        
        petVideo.userImage = model.cover_pic;
        petVideo.userName = [model.user objectForKey:@"screen_name"];
        petVideo.userFeel = model.caption;
        petVideo.videoID = [NSString stringWithFormat:@"%@", self.ID];
        [self.coreDataManager saveContext];
        
        UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"收藏成功" message:nil preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *alertAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
            NSLog(@"  ");
        }];
        [alertController addAction:alertAction];
        [self presentViewController:alertController animated:YES completion:nil];
        
    }
    
    if ([[NSUserDefaults standardUserDefaults] boolForKey:[NSString stringWithFormat:@"%@", self.ID]]) {
        UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"收藏过了" message:nil preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *alertAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
            NSLog(@"  ");
        }];
        [alertController addAction:alertAction];
        [self presentViewController:alertController animated:YES completion:nil];
    }
}
- (void)sendPlayer:(AVPlayer *)player{
    self.player = player;
}
#pragma mark 数据解析
- (void)loadDataWithString:(NSString *)urlString{
    [ZFHYNetWork handleGETWithUrlString:urlString parameters:nil showHuD:YES onView:nil successfulBlock:^(id responseObject) {
       self.medioDic = responseObject;
        [self.tableView reloadData];
    } failureBlock:^(NSError *error) {
        NSLog(@"失败");
    }];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    ZCXVedioDetialTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ZCXvediodetialtableviewcell"];
    cell.delegate = self;
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.vedioDetialModel = [ZCXVedioDetialModel ZCXVedioDetialModelWithDictionary:_medioDic];
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return self.view.frame.size.height - 113 * HEIGHT;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 1;
}
#pragma mark 代理方法
- (void)pushIntoUserDetial:(NSString *)str titleName:(NSString *)titleName{
    [self.player pause];
    ZCXUserDetialViewController *userDetial = [[ZCXUserDetialViewController alloc]init];
    userDetial.strID = str;
    userDetial.titleName = titleName;
    self.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:userDetial animated:YES];
    [userDetial release];
}
- (void)viewWillDisappear:(BOOL)animated{
    [SVProgressHUD dismiss];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
